package com.hsl.housaileibot001.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hsl.housaileibot001.mapper.MockExamGenerationTaskMapper;
import com.hsl.housaileibot001.model.MockExamGenerationTask;
import com.hsl.housaileibot001.service.MockExamGenerationTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 模拟题生成任务服务实现类
 */
@Service
public class MockExamGenerationTaskServiceImpl implements MockExamGenerationTaskService {

    @Autowired
    private MockExamGenerationTaskMapper taskMapper;

    @Override
    @Transactional
    public MockExamGenerationTask createTask(MockExamGenerationTask task) {
        task.setCreatedTime(LocalDateTime.now());
        task.setUpdatedTime(LocalDateTime.now());
        task.setStatus("PENDING");
        task.setCompletedCount(0);
        task.setFailedCount(0);
        // 设置 id 为 taskId 的 hash 值
        if (task.getTaskId() != null) {
            task.setId((long) task.getTaskId().hashCode());
        }
        taskMapper.insert(task);
        return task;
    }

    @Override
    public MockExamGenerationTask getTaskById(String taskId) {
        com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<MockExamGenerationTask> wrapper = 
                new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
        wrapper.eq(MockExamGenerationTask::getTaskId, taskId);
        return taskMapper.selectOne(wrapper);
    }

    @Override
    @Transactional
    public void updateTaskStatus(String taskId, String status) {
        LambdaUpdateWrapper<MockExamGenerationTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MockExamGenerationTask::getTaskId, taskId)
                .set(MockExamGenerationTask::getStatus, status)
                .set(MockExamGenerationTask::getUpdatedTime, LocalDateTime.now());
        if ("PROCESSING".equals(status)) {
            updateWrapper.set(MockExamGenerationTask::getStartTime, LocalDateTime.now());
        }
        taskMapper.update(null, updateWrapper);
    }

    @Override
    @Transactional
    public void incrementCompleted(String taskId) {
        LambdaUpdateWrapper<MockExamGenerationTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MockExamGenerationTask::getTaskId, taskId)
                .setSql("completed_count = completed_count + 1")
                .set(MockExamGenerationTask::getUpdatedTime, LocalDateTime.now());
        taskMapper.update(null, updateWrapper);
    }

    @Override
    @Transactional
    public void incrementFailed(String taskId) {
        LambdaUpdateWrapper<MockExamGenerationTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MockExamGenerationTask::getTaskId, taskId)
                .setSql("failed_count = failed_count + 1")
                .set(MockExamGenerationTask::getUpdatedTime, LocalDateTime.now());
        taskMapper.update(null, updateWrapper);
    }

    @Override
    @Transactional
    public void completeTask(String taskId) {
        LambdaUpdateWrapper<MockExamGenerationTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MockExamGenerationTask::getTaskId, taskId)
                .set(MockExamGenerationTask::getStatus, "COMPLETED")
                .set(MockExamGenerationTask::getEndTime, LocalDateTime.now())
                .set(MockExamGenerationTask::getUpdatedTime, LocalDateTime.now());
        taskMapper.update(null, updateWrapper);
    }

    @Override
    @Transactional
    public void failTask(String taskId, String errorMessage) {
        LambdaUpdateWrapper<MockExamGenerationTask> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MockExamGenerationTask::getTaskId, taskId)
                .set(MockExamGenerationTask::getStatus, "FAILED")
                .set(MockExamGenerationTask::getEndTime, LocalDateTime.now())
                .set(MockExamGenerationTask::getErrorMessage, errorMessage)
                .set(MockExamGenerationTask::getUpdatedTime, LocalDateTime.now());
        taskMapper.update(null, updateWrapper);
    }
}

